package com.paper.tiger.easy.rpc.center;

import com.paper.tiger.easy.rpc.core.GeneralHandleData;
import com.paper.tiger.easy.rpc.core.ServerType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * [简要描述]: 启动注册中心服务, 提供两个功能，1.注册服务，2.
 * [详细描述]:
 *
 * @author ZouHaiBo
 * @version 1.0, 2021/5/31 08:50
 * @since JDK 1.8
 */
public class Start
{


    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(2,10,60, TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(1000));


    private static Map<String,BasicService> executeClass = new HashMap<>();


    public static void main(String[] args) throws IOException
    {

        registerClass();
        ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress("127.0.0.1",6666));


        while (true){
            System.out.println("服务已经启动，开始监听请求");
            Socket socket = serverSocket.accept();
            System.out.println("-----------接收到客户端的注册请求-------------");
            executor.execute(new ReadableSocket(socket));
        }


    }


    public static class ReadableSocket implements Runnable{

        private Socket socket;

        public ReadableSocket(Socket socket)
        {
            this.socket = socket;
        }

        @Override
        public void run()
        {

            ObjectInputStream inputStream = null;
            ObjectOutputStream outputStream = null;
            try
            {
                inputStream = new ObjectInputStream(socket.getInputStream());
                outputStream = new ObjectOutputStream(socket.getOutputStream());

                GeneralHandleData generalHandleData = (GeneralHandleData) inputStream.readObject();

                Integer code = generalHandleData.getType();
                ServerType type = ServerType.getByCode(code);
                BasicService basicService = executeClass.get(type.getRegisterName());

                GeneralHandleData service = basicService.service(generalHandleData);

                outputStream.writeObject(service);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }finally
            {
                try
                {
                    socket.close();
                    inputStream.close();
                    outputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

        }
    }


    private static void registerClass()
    {
        // 注册服务
        executeClass.put("register_reqeust",new RegisterService());
        // 查询请求
        executeClass.put("select_request",new SelectService());

    }


}
